package com.itheima.web.controller;

import com.itheima.domain.db.Admin;
import com.itheima.util.ConstantUtil;
import com.itheima.util.ResultInfo;
import com.itheima.web.manager.LoginManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    private LoginManager loginManager;

    @Autowired
    private HttpSession session;

    //登陆
    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Map<String,String> param){
        //1.接收参数
        String username = param.get("username");
        String password = param.get("password");
        //2.调用manager完成登陆
       ResultInfo resultInfo= loginManager.login(username,password);
        //3.判断，如果登陆成功，将员工信息存储到session
        if (resultInfo.getCode()==1){
            Admin admin = (Admin) resultInfo.getData();
            session.setAttribute(ConstantUtil.SESSION_ADMIN,admin);
        }
        //3.返回admin
        return resultInfo;
    }
    //退出登陆
    @PostMapping("/employee/logout")
    public ResultInfo logout(){
        //1.清空session
        session.invalidate();
        //2.返回成功
        return ResultInfo.success(null);
    }
}
